package com.blockmeta.bbs.minemodule.fragment;


import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.text.TextUtils;
import android.view.View;

import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseCodeResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.DeepLinkUtils;
import com.blockmeta.bbs.minemodule.R;
import com.blockmeta.bbs.minemodule.adapter.MyActiveListMultiAdapter;
import com.blockmeta.bbs.minemodule.net.NetApi;
import com.blockmeta.bbs.minemodule.pojo.IMyActiveListPOJO;
import com.blockmeta.bbs.minemodule.pojo.MyActiveListPOJO;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author airthor
 * @date 2019/12/12
 * 活动列表
 */
public class MyActiveListFragment extends BaseMyActiveListFragment<MyActiveListMultiAdapter> implements View.OnClickListener, MyActiveListMultiAdapter.OnAcitiveItemClickListener {

    private static MediaType sMediaType = MediaType.parse("application/json; charset=utf-8");
    private HashMap<String,Object> mParams = new HashMap<>();

    private List<IMyActiveListPOJO> mListPOJOS = new ArrayList<>();
    private String mValue;

    public static Fragment instance(String key,String value){
        MyActiveListFragment fragment = new MyActiveListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.ZERO,key);
        bundle.putString(Constant.ONE,value);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected MyActiveListMultiAdapter initAdapter() {
        MyActiveListMultiAdapter adapter = new MyActiveListMultiAdapter(mListPOJOS);
        adapter.setOnAcitiveItemClickListener(this);
        return adapter;
    }

    @Override
    public void initView() {
        Bundle arguments = getArguments();
        if (arguments == null){return;}
        String key = arguments.getString(Constant.ZERO);
        mValue = arguments.getString(Constant.ONE);
        if (!TextUtils.isEmpty(key)) {
            mParams.put(key, mValue);
        }
        netWork(false);
    }
    /** 请求列表数据 */
    private void netWork(final boolean isRefresh) {
//        mParams.put("page",mPageNo);
//        RequestBody requestBody = RequestBody.create(sMediaType,new Gson().toJson(mParams));
//        RetrofitFactory.getRootRetrofit().create(NetApi.MyActiveList.class)
//                .myActiveList(requestBody)
//                .compose(this.<BaseCodeResponse<MyActiveListPOJO>>bindToLifecycle())
//                .compose(RxUtil.<BaseCodeResponse<MyActiveListPOJO>>rxSchedulerHelper())
//                .compose(RxUtil.<MyActiveListPOJO>commonResultByCode())
//                .subscribe(new SubscriberForActive<MyActiveListPOJO>(mContext) {
//                    @Override
//                    public void onNext(MyActiveListPOJO activeListPOJOS) {
//                        super.onNext(activeListPOJOS);
//                        if (activeListPOJOS==null||activeListPOJOS.list==null||activeListPOJOS.list.size()==0){
//                            if (mActiveListAdapter.getEmptyView()==null){
//                                setEmptyView();
//                            }
//                        }
//                        if (activeListPOJOS!=null) {
//                            parseData(isRefresh, activeListPOJOS.list);
//                        }
//                    }
//                });
    }

    private void setEmptyView() {
        View empty = getLayoutInflater().inflate(R.layout.empty_my_active,null);
        empty.findViewById(R.id.btn_go_active).setOnClickListener(this);
        mActiveListAdapter.setEmptyView(empty);
    }

    private void parseData(boolean isRefresh,List<MyActiveListPOJO.ListBean> list) {
        if (list==null||list.size()==0){
            if (isRefresh){
                mListPOJOS.clear();
                mActiveListAdapter.notifyDataSetChanged();
            }
            return;
        }
        if (isRefresh){
            mListPOJOS.clear();
        }
        mListPOJOS.addAll(list);
        mActiveListAdapter.notifyDataSetChanged();
    }

    /**
     * 上拉加载更多
     */
    @Override
    public void onLoadMoreRequested() {
        netWork(false);
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        super.onRefresh(refreshlayout);
        netWork(true);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.btn_go_active){
            DeepLinkUtils.jumpToTarget(mContext, Uri.parse("blockmeta://activity"));
        }
    }

    @Override
    public void onActiveItemClickListener(IMyActiveListPOJO item) {
        if (item.iSActiveOverdue()){
            RouterHelper.startActivity(mContext,RouterPath.ACTIVE_DETAIL_ACTIVITY,item.getTid());
        }else if ("checking".equals(mValue)){
            RouterHelper.startActivity(mContext,RouterPath.ACTIVE_DETAIL_ACTIVITY,item.getTid());
        }else if (!TextUtils.isEmpty(item.getCode())){
            RouterHelper.startActivity(mContext,RouterPath.ERWEIMA_ACTIVITY,item.getTid(),item.getCode());
        }
    }
}
